<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>&#x53D6;&#x503C;/&#x8D4B;&#x503C; | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>&#x53D6;&#x503C;/&#x8D4B;&#x503C;</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>取值/赋值</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/get-set/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/02-get-set/00-intro.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
<p>Moment.js &#x4F7F;&#x7528;&#x91CD;&#x8F7D;&#x7684; getter &#x548C; setter &#x65B9;&#x6CD5;&#x3002;
&#x6B64;&#x6A21;&#x5F0F;&#x7C7B;&#x4F3C;&#x4E0E;&#x5176;&#x5728; jQuery &#x4E2D;&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4E0D;&#x5E26;&#x53C2;&#x6570;&#x8C03;&#x7528;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x4F1A;&#x4F5C;&#x4E3A; getter&#xFF0C;&#x800C;&#x5E26;&#x53C2;&#x6570;&#x8C03;&#x7528;&#x5219;&#x4F1A;&#x4F5C;&#x4E3A; setter&#x3002;</p>
<p>&#x8FD9;&#x4E9B;&#x4F1A;&#x6620;&#x5C04;&#x5230;&#x539F;&#x751F; <code>Date</code> &#x5BF9;&#x8C61;&#x4E0A;&#x7684;&#x76F8;&#x5E94;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="language-javascript">moment().seconds(30).valueOf() === new Date().setSeconds(30);
moment().seconds()   === new Date().getSeconds();
</code></pre>
<p>&#x5982;&#x679C;&#x5904;&#x4E8E; <a href="javascript:window.open('http://nodejs.cn/s/t4J2Y4');" target="_blank" rel="nofollow">UTC &#x6A21;&#x5F0F;</a>&#x4E2D;&#xFF0C;&#x5219;&#x5B83;&#x4EEC;&#x5C06;&#x4F1A;&#x6620;&#x5C04;&#x5230; UTC &#x7684;&#x7B49;&#x6548;&#x9879;&#x3002;</p>
<pre><code class="language-javascript">moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30);
moment.utc().seconds()   === new Date().getUTCSeconds();
</code></pre>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x8D77;&#x89C1;&#xFF0C;&#x4ECE; <strong>2.0.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;&#x5355;&#x6570;&#x548C;&#x590D;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#x540D;&#x79F0;&#x90FD;&#x4F1A;&#x5B58;&#x5728;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5F53;&#x4F5C;&#x4E3A; setter &#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x5728;&#x90FD;&#x4F1A;&#x6539;&#x53D8;&#x539F;&#x59CB;&#x7684; moment&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4ECE; <strong>2.19.0</strong> &#x5F00;&#x59CB;&#xFF0C;&#x5C06; <code>NaN</code> &#x4F20;&#x7ED9;&#x4EFB;&#x4F55; setter &#x90FD;&#x662F;&#x6CA1;&#x6709;&#x64CD;&#x4F5C;&#x7684;&#x3002;
&#x5728; <strong>2.19.0</strong> &#x4E4B;&#x524D;&#xFF0C;&#x5B83;&#x4EE5;&#x9519;&#x8BEF;&#x7684;&#x65B9;&#x5F0F;&#x4F7F; moment &#x65E0;&#x6548;&#x3002;</p>
					</div>
          <div id="content_right">
<p>Moment.js uses overloaded getters and setters. You may be familiar with this pattern from its use in jQuery.</p>
<p>Calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.</p>
<p>These map to the corresponding function on the native <code>Date</code> object.</p>
<pre><code class="language-javascript">moment().seconds(30).valueOf() === new Date().setSeconds(30);
moment().seconds()   === new Date().getSeconds();
</code></pre>
<p>If you are in <a href="#/manipulating/utc/" target="_blank" rel="nofollow">UTC mode</a>, they will map to the UTC equivalent.</p>
<pre><code class="language-javascript">moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30);
moment.utc().seconds()   === new Date().getUTCSeconds();
</code></pre>
<p>For convenience, both singular and plural method names exist as of version <strong>2.0.0</strong>.</p>
<p><strong>Note:</strong> All of these methods mutate the original moment when used as setters.</p>
<p><strong>Note:</strong> From <strong>2.19.0</strong> passing <code>NaN</code> to any setter is a no-op. Before
<strong>2.19.0</strong> it was invalidating the moment in a wrong way.</p>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>